﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;
using googlemaps;
namespace ClientMobile
{
    public partial class Map : PhoneApplicationPage
    {
        GeoCoordinateWatcher watcher;
        public GeoCoordinate Coords;
        public Map()
        {
            InitializeComponent();
            hybrid.Visibility = Visibility.Visible;
            satellite.Visibility = Visibility.Collapsed;
            street.Visibility = Visibility.Collapsed;
            physical.Visibility = Visibility.Collapsed;
            wateroverlay.Visibility = Visibility.Collapsed;
            googlemap.ZoomLevel = 11;
            googlemap.CopyrightVisibility = Visibility.Collapsed;
            googlemap.LogoVisibility = Visibility.Collapsed;
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default)
            {
                MovementThreshold = 20
            };
            watcher.PositionChanged += this.watcher_PositionChanged;
            watcher.StatusChanged += this.watcher_StatusChanged;
            watcher.Start();
        }
        private void ButtonZoomIn_Click(object sender, RoutedEventArgs e)
        {
            googlemap.ZoomLevel++;
        }

        private void ButtonZoomOut_Click(object sender, RoutedEventArgs e)
        {
            googlemap.ZoomLevel--;

        }
        private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    // location is unsupported on this device
                    break;
                case GeoPositionStatus.NoData:
                    // data unavailable
                    break;
            }
        }

        private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            var epl = e.Position.Location;
            Coords = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
            googlemap.SetView(Coords, 11);
            
            // Access the position information thusly:
            //epl.Latitude.ToString("0.000");
            //epl.Longitude.ToString("0.000");
            //epl.Altitude.ToString();
            //epl.HorizontalAccuracy.ToString();
            //epl.VerticalAccuracy.ToString();
            //epl.Course.ToString();
            //epl.Speed.ToString();
            //e.Position.Timestamp.LocalDateTime.ToString();
            Pushpin pin = new Pushpin();
            Singleton s = Singleton.Instance;
            ImageBrush image = new ImageBrush()
            {
                ImageSource = new System.Windows.Media.Imaging.BitmapImage
                                  (new Uri(s.Chauff.Image))
            };
            //---draw an ellipse inside the pushpin and fill it with the image---

            pin.Content = new Rectangle()
            {
                Fill = image,
                StrokeThickness = 10,
                Height = 89,
                Width = 66

            };
            pin.Location = Coords;
            googlemap.Children.Add(pin);
        }
    }
}